package com.szpx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szpx.entity.Vip;
import com.szpx.service.VipManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class VipManageController {

    @Autowired
    private VipManageService vipManageService;

    /**
     *  根据id查询影片的信息
     */

    @GetMapping("/selectVipById/{id}")
    public String selectVipById(@PathVariable("id") Integer id, Model model) {
        Vip vip = vipManageService.selectVipById(id);
        if (vip != null){
            model.addAttribute("vip",vip);
            return "zx/zx_VipManageHtml/zx_VipDetail";
        }else {
            return null;
        }
    }




    /**
     * 删除影片信息
     */
    @RequestMapping("/deleVip/{id}")
    public String deleVip(@PathVariable("id")Integer id){
        int num = vipManageService.deleVip(id);
        if (num > 0)
            return "redirect:/plist"; //重定向到影片列表的请求
        else
            return null;
    }

    /**
     * 去影片修改页面
     */
    @GetMapping("/toUpdateVip/{id}")
    public String toUpdateVipPage(@PathVariable("id")Integer id
            ,Model model){
        Vip vip = vipManageService.selectVipById(id);
        if (vip != null){
            model.addAttribute("vip",vip); //将查询出来的信息 传到修改页面
            return "zx/zx_VipManageHtml/zx_VipUpdate";
        }else {
            return null;
        }
    }

    /**
     * 修改影片信息
     */
    @RequestMapping("/updateVip")
    public String updateVip(Vip vip){
        int num = vipManageService.updateVip(vip);
        if (num > 0)
            return "redirect:/plist";
        else
            return null;
    }

    /**
     * 添加影片
     */
    @RequestMapping("/addVip")
    public String addVip(Vip vip){
        int num = vipManageService.addVip(vip);
        if (num > 0)
            return "redirect:/plist";
        else
            return null;
    }

    /**
     * 跳转到添加影片界面
     */
    @RequestMapping("/toAddVip")
    public String toAddVip(HttpSession session){
        return "zx/zx_VipManageHtml/zx_VipAdd";
    }


    /**
     * 多条件分页查询所有影片
     */
    @GetMapping("/plist")
    public String getPlist(
            @RequestParam(defaultValue = "1") int pno,
            @RequestParam(defaultValue = "3") int pageSize,
            Vip vip, Model model){
        PageHelper.startPage(pno,pageSize);//开始分页
        List<Vip> plist = vipManageService.getListByInfo(vip);
        PageInfo<Vip> page = new PageInfo<Vip>(plist);
        model.addAttribute("plist",plist); //将查询到的数据放入model对象中
        model.addAttribute("page",page);
        model.addAttribute("name",vip.getName());
        model.addAttribute("number",vip.getNumber());
        model.addAttribute("pno",pno);
        return "zx/zx_VipManageHtml/zx_VipVlist";
    }

//    /**
//     * 访问模板逻辑视图model
//     * @param model
//     * @return
//     */
//    @GetMapping("/model")
//    public String model(Model model) {
//        return "model"; //逻辑视图
//    }
}
